﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Office.Interop.MSProject;
using System.Reflection;

namespace Qpm.Automap.Core
{
  public static class ProjectAutomator
  {

    public static void ConvertMppToXml(string projectMppFilename, string exportFilename)
    {
      ApplicationClass projectApplication = new ApplicationClass();
      projectApplication.FileOpen(
          projectMppFilename,        // object Name
          true,                      // object ReadOnly  
          Missing.Value,             // object Merge
          Missing.Value,             // object taskInfomation
          Missing.Value,             // object Table
          Missing.Value,             // object Sheet
          Missing.Value,                      // object NoAuto
          Missing.Value,             // object UserId
          Missing.Value,             // object DatabasePassWord
          Missing.Value,             // object FormatID
          Missing.Value,             // object Map
          PjPoolOpen.pjPoolReadOnly, // object openPool
          Missing.Value,             // object Password
          Missing.Value,             // object WriteResPassword
          Missing.Value,             // object IgnoreReadOnlyRecommended
          Missing.Value);            // object XmlName

      projectApplication.FileSaveAs(
          exportFilename,            // object Filename
          PjFileFormat.pjMPP,        // PjFileFormat Format  
          Missing.Value,             // object Backup
          false,                     // object ReadOnly  
          Missing.Value,             // object taskInfomation
          Missing.Value,             // object Filtered
          Missing.Value,             // object Table
          Missing.Value,             // object UserId
          Missing.Value,             // object DatabasePassWord
          "MSProject.XML",           // object FormatID
          Missing.Value,             // object Map
          Missing.Value,             // object Password
          Missing.Value,             // object WriteResPassword
          Missing.Value,             // object ClearBaseline
          Missing.Value,             // object ClearActuals
          Missing.Value,             // object ClearResourceRates
          Missing.Value,             // object ClearFixedCosts
          Missing.Value,             // object XmlName
          Missing.Value);            // object ClearConfirmed
      projectApplication.FileClose(PjSaveType.pjDoNotSave, Missing.Value);

      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.Load(exportFilename);
      XmlElement root = xmlDoc.DocumentElement;
      XmlElement customPropertiesNode = xmlDoc.CreateElement("CustomDocumentProperties");
      root.AppendChild(customPropertiesNode);

      //DSOFile.OleDocumentPropertiesClass pDocument = new DSOFile.OleDocumentPropertiesClass();
      //pDocument.Open(projectMppFilename, true, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
      //foreach (DSOFile.CustomProperty prop in pDocument.CustomProperties)
      //{
      //  if (prop.Name != null && prop.Name.Trim().Length>0)
      //    AddCustomProperty(xmlDoc, customPropertiesNode, prop.Name, prop.get_Value().ToString(), prop.Type);
      //}
      xmlDoc.Save(exportFilename);

      //      We need to read the custom document properties and insert them into the 
      //      xml file. They should be in xml format like the following:
      //  
      //      <CustomDocumentProperties>
      //          <PropertyName>
      //              PropertyValue
      //          </PropertyName>
      //      </CustomDocumentProperties>
      //      
      //      I had to change the format of the xml because I was having some problems 
      //      with some weird characters in the the property name.  This is the format I used:

      //      <CustomDocumentProperties>
      //        <CustomProperty>
      //          <Name> propertyName </Name>
      //          <Value> propertyValue </Value>
      //          <Type> propertyType </Type>
      //        </CustomProperty>
      //      </CustomDocumentProperties>
    }
    //public static void AddCustomProperty(XmlDocument xmlDoc, XmlElement parentNode, string propertyName, 
    //  string propertyValue, DSOFile.dsoFilePropertyType propertyType)
    //{
    //  XmlElement propNode = xmlDoc.CreateElement("CustomProperty");
    //  XmlElement nameNode = xmlDoc.CreateElement("Name");
    //  XmlElement valueNode = xmlDoc.CreateElement("Value");
    //  XmlElement typeNode = xmlDoc.CreateElement("Type");
    //  switch (propertyType)
    //  {
    //    case DSOFile.dsoFilePropertyType.dsoPropertyTypeBool:
    //      typeNode.InnerText = "Bool";
    //      break;
    //    case DSOFile.dsoFilePropertyType.dsoPropertyTypeDate:
    //      typeNode.InnerText = "Date";
    //      break;
    //    case DSOFile.dsoFilePropertyType.dsoPropertyTypeDouble:
    //      typeNode.InnerText = "Double";
    //      break;
    //    case DSOFile.dsoFilePropertyType.dsoPropertyTypeLong:
    //      typeNode.InnerText = "Long";
    //      break;
    //    case DSOFile.dsoFilePropertyType.dsoPropertyTypeString:
    //      typeNode.InnerText = "String";
    //      break;
    //    case DSOFile.dsoFilePropertyType.dsoPropertyTypeUnknown:
    //      typeNode.InnerText = "Unknown";
    //      break;
    //    default:
    //      break;
    //  }
    //  nameNode.InnerText = propertyName;
    //  valueNode.InnerText = propertyValue;
    //  propNode.AppendChild(nameNode);
    //  propNode.AppendChild(valueNode);
    //  propNode.AppendChild(typeNode);
    //  parentNode.AppendChild(propNode);
    //}
  }
}
